package cn.com.wangluotongxin.merchants.controller;

import cn.com.wangluotongxin.chat.DubboSysUserService;
import cn.com.wangluotongxin.chat.vo.SysUserVo;
import cn.com.wangluotongxin.common.web.AjaxResult;
import cn.com.wangluotongxin.merchants.database.MerchantsDatabaseService;
import cn.com.wangluotongxin.merchants.eureka.form.InitMerchantsUserForm;
import cn.com.wangluotongxin.merchants.service.IMerchantsUserService;
import cn.hutool.core.lang.Assert;
import com.alibaba.dubbo.config.annotation.Reference;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "网络通信- 商户微服务相关接口")
@RestController
@RequestMapping("/inner/merchants")
public class InnerMerchantsController {

    @Autowired
    private MerchantsDatabaseService databaseService;
    @Reference
    private DubboSysUserService sysUserService;
    @Autowired
    private IMerchantsUserService userService;

    @ApiOperation("加载商户资源信息")
    @PostMapping("/load")
    public AjaxResult load(){
        databaseService.loadDatabase();
        return AjaxResult.success();
    }

    @ApiOperation("单独加载商户资源信息")
    @PostMapping("/single/load/{merchantsId}")
    public AjaxResult singleLoad(@PathVariable("merchantsId") Integer merchantsId) {
        databaseService.loadDatabase(merchantsId);
        return AjaxResult.success();
    }

    @ApiOperation("初始化账号")
    @PostMapping("/init/user")
    public AjaxResult initUser(@RequestBody InitMerchantsUserForm form) {
        Assert.notNull(form.getChatUserId(), "未识别到注册人信息");
        SysUserVo sysUserVo = sysUserService.detail(form.getChatUserId());
        Assert.notNull(sysUserVo, "申请账号信息已失效");

        userService.initAdminUser(sysUserVo);
        return AjaxResult.success();
    }
}
